package com.heo.coder.packet.base;

import java.util.Date;

import com.heo.util.ByteHelper;

public abstract class BasePacket {

	public static final int START_MARKER_LENGTH = 1;// 标识位长度
	public static final String START_MARKER_LENGTH_CODE = "68";// 标识位内容
	public static final int CRC_MARKER_LENGTH = 1;// 标识位长度
	public static final int END_MARKER_LENGTH = 1;// 标识位长度
	public static final String END_MARKER_LENGTH_CODE = "16";// 标识位内容
	public static final int CHECK_LENGTH = 1;

	public static final int HEAD_LENGTH = 14;// 消息头长度

	// 消息头
	protected String id = ""; // 消息 ID 4位
	protected String head = ""; //head
	protected String msta = ""; // 加密1位 0不加密
	protected String control = ""; // 加密1位 0不加密
	protected int bodyLength = 0; // 消息体长度 
	protected String bodyLengthStr = ""; // 消息体长度 16进制
	public String body = ""; // 具体数据 16进制数据
	protected String check = ""; // 校验位 长度1 2字节 校验码指从消息头开始，同后一字节异或，直到效验码前一个字节，占用一个字节

	private String macAddress = "";
	protected Date createTime = new Date();


	public int getTotleLength() {
		return START_MARKER_LENGTH + HEAD_LENGTH + bodyLength + CHECK_LENGTH + END_MARKER_LENGTH;
	}

	/**
	 * 根据数据包构造对象
	 */
	public BasePacket(String head, String id, int bodyLength, String msta,
			String control, String body, String check) throws Exception {
		this.head = head;
		this.id = id;
		this.bodyLengthStr = ByteHelper.intToHexString(bodyLength, 4);
		this.bodyLength = bodyLength;
		this.msta = msta;
		this.control = control;
		this.body = body;
		this.check = check;
	}

	public abstract String getPacketBody();

	/**
	 * 获得整个数据包
	 * 
	 * @return
	 */
	public byte[] getPacket() {
		return ByteHelper.margeByte(
				ByteHelper.hexStringToBytes(START_MARKER_LENGTH_CODE), 
				ByteHelper.hexStringToBytes(head),
				ByteHelper.hexStringToBytes(msta), 
				ByteHelper.hexStringToBytes(id), 
				ByteHelper.hexStringToBytes(START_MARKER_LENGTH_CODE), 
				ByteHelper.hexStringToBytes(control), 
				ByteHelper.hexStringToBytes(bodyLengthStr), 
				ByteHelper.hexStringToBytes(body), 
				ByteHelper.hexStringToBytes(check),
				ByteHelper.hexStringToBytes(END_MARKER_LENGTH_CODE));
	}

	
	public int getBodyLength() {
		return bodyLength;
	}

	public void setBodyLength(int bodyLength) {
		this.bodyLength = bodyLength;
	}

	
	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getHead() {
		return head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public String getMsta() {
		return msta;
	}

	public void setMsta(String msta) {
		this.msta = msta;
	}

	public String getControl() {
		return control;
	}

	public void setControl(String control) {
		this.control = control;
	}

	public String getMacAddress() {
		return macAddress;
	}

	public void setMacAddress(String macAddress) {
		this.macAddress = macAddress;
	}

	public String getBodyLengthStr() {
		return bodyLengthStr;
	}

	public void setBodyLengthStr(String bodyLengthStr) {
		this.bodyLengthStr = bodyLengthStr;
	}
}
